
#include "vtkImageFilterUsingOpenCV.h"
#include <vtkImageFFT.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <vtkObjectFactory.h>
#include <vtkPointData.h>
#include <vtkUnsignedcharArray.h>
#include <vtkInformationVector.h>
#include <vtkInformation.h>
#include <vtkDataSet.h>
#include <vtkImageData.h>
vtkCxxRevisionMacro(vtkImageFilterUsingOpenCV, "$Revision: 1.00 $");
vtkStandardNewMacro(vtkImageFilterUsingOpenCV);
vtkImageFilterUsingOpenCV::vtkImageFilterUsingOpenCV(void)
{
}

vtkImageFilterUsingOpenCV::~vtkImageFilterUsingOpenCV(void)
{
}

int vtkImageFilterUsingOpenCV::RequestData( vtkInformation* request,
										   vtkInformationVector**  inputVector ,
										   vtkInformationVector* outputVector)
{
	vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
	vtkInformation *outInfo = outputVector->GetInformationObject(0);
	// get the input and ouptut
	vtkDataSet *input = vtkDataSet::SafeDownCast(
		inInfo->Get(vtkDataObject::DATA_OBJECT()));
	vtkDataSet *output = vtkDataSet::SafeDownCast(
		outInfo->Get(vtkDataObject::DATA_OBJECT()));

	output->DeepCopy(input);
	vtkImageData * inputImage = vtkImageData::SafeDownCast(input);
	vtkImageData * outputImage = vtkImageData::SafeDownCast(output);
	

	if (inputImage && outputImage)
	{
		int dim[3];
		inputImage->GetDimensions(dim);
		int width = dim[0];
		int height = dim[1];
		vtkPointData * pointData = inputImage->GetPointData();
		if (pointData)
		{
			vtkDataArray * scalar = pointData->GetScalars();
			if (scalar)
			{
				if (vtkUnsignedCharArray::SafeDownCast(scalar))
				{
				
					//unsigned char* rawPointer1 = reinterpret_cast<unsigned char*>(scalar->GetVoidPointer(0));
					unsigned char* rawPointer2 = reinterpret_cast<unsigned char*>(
								vtkUnsignedCharArray::SafeDownCast(outputImage->GetPointData()->GetScalars())->GetVoidPointer(0));
					for (int i = 0; i < width; ++i)
						for (int j = 0; j < height; ++j)
						{
							unsigned char t;
							t = rawPointer2[j * width * 3 + 3 * i + 0];
							rawPointer2[j * width * 3 + 3 * i + 0] = rawPointer2[j * width * 3 + 3 * i + 2];
							rawPointer2[j * width * 3 + 3 * i + 2] = t;
						}
					
						IplImage * temImage =  cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 3);
						temImage->origin = 1;
						cvSetData(temImage, rawPointer2, temImage->widthStep);

						//here are some processing using OpenCV
						//cvLine(temImage, cvPoint(0, 0), cvPoint(200, 200), cvScalar(255, 255));
						cvPutText(temImage, "This image is processed by OpenCV", cvPoint(5, 100), &cvFont(1), cvScalar(0, 255));
					
					for (int i = 0; i < width; ++i)
						for (int j = 0; j < height; ++j)
						{
							unsigned char t;
							t = rawPointer2[j * width * 3 + 3 * i + 0];
							rawPointer2[j * width * 3 + 3 * i + 0] = rawPointer2[j * width * 3 + 3 * i + 2];
							rawPointer2[j * width * 3 + 3 * i + 2] = t;
						}
					cvReleaseImageHeader(&temImage);
				}
			}
		}
		
	}


	


	return 1;
	

}
